package m.app.action.query;

import com.alibaba.fastjson.JSONObject;
import m.app.action.AppAction;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQuery;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.store.model.GoodsOutRecord;
import m.store.model.PayAmountRecord;
import m.store.model.SaleInfo;
import m.store.model.StockInfo;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentParamMeta;
import m.system.util.JSONMessage;

import java.util.List;

@ActionMeta(name = "app/query/payAmount",title = "app查询付款信息")
public class PayAmountAction extends AppAction {
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "查询付款订单",permission = true),
        params = {
            @DocumentParamMeta(name = "page.index",description = "分页开始位置",type = FieldType.INT),
            @DocumentParamMeta(name = "page.num",description = "分页每页数量",type = FieldType.INT),
            @DocumentParamMeta(name = "params[oid@eq]",description = "条件示例，字段和操作符中间用@，值为空不添加",type = FieldType.STRING,length = 50),
            @DocumentParamMeta(name = "params[oid#desc]",description = "排序示例，字段和操作符中间用#，值为空不生效",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage queryOrderList() throws Exception {
        ModelQueryList queryList=toQueryList(PayAmountRecord.class,"*","pay.*","pay.guest.*","stock.*");
        queryList.addCondition(QueryCondition.eq("pay.store.oid", PowerUtil.getSessionStoreOid()));
        queryList.addCondition(QueryCondition.eq("pay.status", StockInfo.Status_Done));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "查询付款汇总数据",permission = true),
        params = {
            @DocumentParamMeta(name = "dates",description = "查询付款时间",type = FieldType.STRING)
        }
    )
    @RequestMeta(params = {"dates"})
    public JSONMessage queryCollect(String[] dates) throws Exception {
        StringBuilder instr= new StringBuilder();
        for(String d : dates){
            instr.append(",?");
        }
        List<JSONObject> ls= ModelQuery.init(PayAmountRecord.class)
            .addFieldExp("groupName","date_format(#{pay.payDate},'%Y-%m-%d')")
            .addCollectExp("sumAmount","sum(#{payAmount})")
            .addCondition(QueryCondition.exp("date_format(#{pay.payDate},'%Y-%m-%d') in("+instr.substring(1)+")",(Object[])dates))
            .addCondition(QueryCondition.eq("pay.store.oid", PowerUtil.getSessionStoreOid()))
            .addCondition(QueryCondition.eq("pay.status",SaleInfo.Status_Done))
            .queryCollect();
        return JSONMessage.success("查询成功").push("list",ls);
    }
}
